package com.example.testapplication;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

public class ActivityWebView extends AppCompatActivity {
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        mWebView = findViewById(R.id.webview);

        WebView.setWebContentsDebuggingEnabled(true);
        // 加载网页
       // mWebView.loadUrl("http://www.baidu.com/");
        // 加载sd卡文件
        // mWebView.loadUrl("file://" + Environment.getExternalStorageDirectory().getAbsolutePath() +"/1/index.html");

        // 资源文件的html
        //mWebView.loadUrl("file:///android_asset/html/index.html");
        //mWebView.setWebViewClient(new WebViewClient());

        // 加载http资源
        //mWebView.loadUrl("http://192.168.0.103:8080/example/index.html");

       // mWebView.loadData("<h1>hello WebView</h1>","text/html","utf-8");
        //  http://www.imooc.com/static/img/index/logo.png
        // a标签跳转到百度后，后退的操作会跳到http://www.sogou.com/
       // mWebView.loadDataWithBaseURL("http://www.imooc.com/",
              //  "<img src=\"static/img/index/logo.png\"/><a href=\"http://www.baidu.com/\">tobaibu.com</a>",
               // "text/html","utf-8","http://www.imooc.com/");



//        mWebView.setWebViewClient(new WebViewClient(){
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view,String url){
////                Map<String,String> reqHeaders = new HashMap<>();
////                reqHeaders.put("android-webview-demo","testReqHeaders");
////                mWebView.loadUrl("http://192.168.0.103:8080/example/index.html",reqHeaders);
//                return super.shouldOverrideUrlLoading(view,url);
//            }
//        });
        mWebView.loadUrl("http://www.imooc.com/");
        mWebView.setWebViewClient(new WebViewClient(){

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Log.e("WebViewActivity","webview-》onReceivedError : 加载了url：" + errorCode + " - 错误描述：" + description+ " - 错误代码：" + errorCode);
                view.loadUrl("http://www.imooc.com/");
            }


            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                Log.e("WebViewActivity","webview-》onReceivedError (android6.0以上调用) : 加载了url：" + request.getUrl().toString() + " - 错误描述：" + error.getDescription()+ " - 错误代码：" + error.getErrorCode());
            }

            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                Log.e("WebViewActivity","webview-》shouldInterceptRequest请求了(android5.0之上调用)url：" + request.getUrl().toString());
                return super.shouldInterceptRequest(view, request);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.e("WebViewActivity","webview-》onPageStarted 网页开始进行加载url：" + url);
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
                Log.e("WebViewActivity","webview-》onLoadResource 网页开始加载资源url：" + url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.e("WebViewActivity","webview-》onPageFinished 网页已经加载完成url：" + url);
            }
        });

        // 控制JS运行
        WebSettings webSettings =mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(true);

    }

    public void onBack(View view){
        mWebView.goBack();
    }

        public void onCanGoBack (View v) {
        Toast.makeText(this, String.valueOf(mWebView.canGoBack()), Toast.LENGTH_SHORT).show();
    }

    public void onGoBack (View v) {
        mWebView.goBack();
    }

    public void onCanGoForward (View v) {
        Toast.makeText(this, String.valueOf(mWebView.canGoForward()), Toast.LENGTH_SHORT).show();
    }

    public void onGoForward (View v) {
        mWebView.goForward();
    }

    public void onCanGoBackOrForward (View v) {
        int steps = Integer.valueOf(((EditText)findViewById(R.id.steps)).getText().toString());
        Toast.makeText(this, String.valueOf(mWebView.canGoBackOrForward(steps)), Toast.LENGTH_SHORT).show();
    }

    public void onGoBackOrForward (View v) {
        int steps = Integer.valueOf(((EditText)findViewById(R.id.steps)).getText().toString());
        mWebView.goBackOrForward(steps);
    }

    public void onClearHistory (View v) {
        mWebView.clearHistory();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 暂停所有操作
        mWebView.onPause();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebView.destroy();
    }
}